//
//  GuestureVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/29.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "GuestureVC.h"
#import "DBGuestureLock.h"
#import "GuestureLoginVC.h"

@interface GuestureVC ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation GuestureVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    [self setNavView];
    
    self.view.backgroundColor = Base_color;
    
    [DBGuestureLock clearGuestureLockPassword];
    
    DBGuestureLock *lock = [DBGuestureLock lockOnView:self.view onPasswordSet:^(DBGuestureLock *lock, NSString *password) {
        
        lock.firstTimeSetupPassword = password;
        self.label.text = @"请再次确认手势密码";
        
    } onGetCorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        
        // 密码设置成功
        
        [SVProgressHUD showSuccessWithStatus:@"手势密码设置成功"];
        [SVProgressHUD dismissWithDelay:1.5f];
        
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSString *phoneNum = [[NSUserDefaults standardUserDefaults] objectForKey:USER_PHONE_NUM];
        [defaults setValue: password forKey:[NSString stringWithFormat:@"%@_%@", DBGuestureLockPaswdResult, phoneNum]];
        
        [self performSelector:@selector(successHundle) withObject:password afterDelay:1.5f];
        
    } onGetIncorrectPswd:^(DBGuestureLock *lock, NSString *password) {
        
        if (!lock.firstTimeSetupPassword) {
            [SVProgressHUD showInfoWithStatus:@"密码过于简单 请重新设置"];
            [SVProgressHUD dismissWithDelay:1.5f];
        }else {
            [SVProgressHUD showInfoWithStatus:@"两次密码输入不一致"];
            [SVProgressHUD dismissWithDelay:1.5f];
        }
    }];
    
    [self.view addSubview:lock];
    
    _label = [[UILabel alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight + (lock.top-SafeAreaTopHeight-30)/2.0f, GET_SCREEN_WIDTH(), 30)];
    _label.text = @"请设置手势密码";
    _label.textColor = UICOLOR_HEX(0xffffffff);
    _label.font = [UIFont systemFontOfSize:16];
    _label.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:_label];
}



- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    
    [self.navigationController setNavigationBarHidden:YES animated:NO];
    self.navigationController.navigationBar.alpha = 0;
    [self.navigationController.navigationBar setBackgroundColor:[UIColor clearColor]];
}

#pragma mark - 自定义导航栏
- (void)setNavView {
    
    UIView *navView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), SafeAreaTopHeight)];
    navView.backgroundColor = Base_color;
    [self.view addSubview:navView];
    
    UIButton *backBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, SafeAreaTopHeight-44, 44, 44)];
    [backBtn setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
    [backBtn addTarget:self action:@selector(leftNavigationBarButtonClick) forControlEvents:UIControlEventTouchUpInside];
    [navView addSubview:backBtn];
    
    if (self.isFirstSetGuesturePs) {
        backBtn.hidden = YES;
    }
    
    UILabel *navTitleL = [[UILabel alloc] initWithFrame:CGRectMake(backBtn.right+10, SafeAreaTopHeight-44, GET_SCREEN_WIDTH()-44*2-20, 44)];
    navTitleL.text = @"手势密码";
    navTitleL.textColor = UICOLOR_HEX(0xffffffff);
    navTitleL.textAlignment = NSTextAlignmentCenter;
    navTitleL.font = [UIFont systemFontOfSize:16];
    [navView addSubview:navTitleL];
}

- (void)successHundle {
    
    if (self.isFirstSetGuesturePs) {
        [self firstSetGuesturePassWordSuccessedCallBack];
    }else {
        [self setGuesturePassWordSuccessedCallBack];
    }
}

/**
 * 第一次设置手势密码成功
 */
- (void)firstSetGuesturePassWordSuccessedCallBack {
    
    if ([_delegate respondsToSelector:@selector(LoginSuccessCallBack)]) {
        
        [_delegate LoginSuccessCallBack];
    }
}

/**
 * 修改手势密码成功
 */
- (void)setGuesturePassWordSuccessedCallBack {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"CHANGEGUESTUREPSSUCCESSED" object:nil];
}

@end
